﻿using Sw.Hospital.HealthExaminationSystem.Application.ItemGroup;
using Sw.Hospital.HealthExaminationSystem.Application.ItemGroup.Dto;
using Sw.Hospital.HealthExaminationSystem.Common.Bases;
using Sw.Hospital.HealthExaminationSystem.Common.UserCache;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Sw.Hospital.HealthExaminationSystem.UserSettings.ItemGroup
{
    public partial class frmZKBJ : UserBaseForm
    {
        private IItemGroupAppService itemGroupApp = new ItemGroupAppService();
        private Guid _id;
        private ShowGroupZKDto ShowGroupZK;
        public frmZKBJ()
        {
            InitializeComponent();
        }
        public frmZKBJ(Guid ind) :this()
        { _id = ind; }
        private void frmZKBJ_Load(object sender, EventArgs e)
        {
            dateEdit1.Properties.MinValue = DateTime.Now;
            dateEdit2.Properties.MinValue = DateTime.Now;

            var itemlis = DefinedCacheHelper.GetItemGroups();
            searchLookUpEdit1.Properties.DataSource = itemlis;
            if (_id != null && _id != Guid.Empty)
            {
                SearchGroupZK searchGroup = new SearchGroupZK();
                searchGroup.IND = _id;
                var group = itemGroupApp.getGroupZK(searchGroup);
                ShowGroupZK = group.FirstOrDefault();
                if (ShowGroupZK != null)
                {
                    searchLookUpEdit1.EditValue = ShowGroupZK.GroupId;
                    dateEdit1.EditValue = ShowGroupZK.StarTime;
                    dateEdit2.EditValue = ShowGroupZK.EndTime;
                    teMaxDiscount.EditValue = ShowGroupZK.GroupZK;
                    if (ShowGroupZK.ISQY == 1)
                    {
                        checkEdit1.Checked = true;

                    }
                    else
                    { checkEdit1.Checked = false; }
                }
            }
            else
            {
                checkEdit1.Checked = true;
            }


        }

        private void simpleButton1_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(searchLookUpEdit1.EditValue?.ToString()))
            {
                MessageBox.Show("请选择组合");
                return;
            }
            if (string.IsNullOrEmpty(teMaxDiscount.EditValue?.ToString()))
            {
                MessageBox.Show("折扣不能为空");
                return;
            }
            if (decimal.TryParse(teMaxDiscount.EditValue?.ToString(), out decimal Discount) && Discount >= 1)
            {
                MessageBox.Show("折扣不能大于100%");
                return;
            }
            if (string.IsNullOrEmpty(dateEdit1.EditValue?.ToString()) || string.IsNullOrEmpty(dateEdit2.EditValue?.ToString()) )
            {
                MessageBox.Show("开始时间结束时间不能为空!");
                return;
            }
            if (!string.IsNullOrEmpty(dateEdit1.EditValue?.ToString()) && !string.IsNullOrEmpty(dateEdit2.EditValue?.ToString()) &&
               dateEdit2.DateTime< dateEdit1.DateTime)
            {
                MessageBox.Show("开始时间不能大于结束时间!");
                return;
            }
            GroupZKDto groupZKDto = new GroupZKDto();
            if (_id != null)
            { groupZKDto.Id = _id; }
            groupZKDto.GroupId = (Guid)searchLookUpEdit1.EditValue;
            
            groupZKDto.StarTime = dateEdit1.DateTime;
            groupZKDto.EndTime = dateEdit2.DateTime;
            groupZKDto.GroupZK = decimal.TryParse(teMaxDiscount.EditValue?.ToString(), out decimal maxDiscount) ? (decimal?)maxDiscount : null;
            if (checkEdit1.Checked == true)
            { groupZKDto.ISQY = 1; }
            else
            { groupZKDto.ISQY = 0; }
            itemGroupApp.saveGroupZK(groupZKDto);
            this.DialogResult = DialogResult.OK;

        }

        private void simpleButton2_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
